package cn.edu.lingnan.service;

import cn.edu.lingnan.dao.*;
import cn.edu.lingnan.pojo.Score;
import cn.edu.lingnan.pojo.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentServiceMysqlImpl implements StudentService {

    StudentDao studentDao=new StudentDaoMysqlImpl();
    ScoreDao scoreDao =new ScoreDaoMysqlImpl();
    CourseDao courseDao=new CourseDaoMysqlImpl();
    @Override
    public Student findStudentByNameAndPassword(String _sname, String _spassword) {
        return studentDao.findStudentByNameAndPassword(_sname,_spassword);
    }

    @Override
    public List<Student> findStudentAll() {
        return studentDao.findStudentAll();
    }

    @Override
    public Student findStudentBySid(String _sid) {
        return studentDao.findStudentBySid(_sid);
    }

    @Override
    public List<Student> findStudentBySname(String _sname) {
        return studentDao.findStudentBySname(_sname);
    }

    @Override
    public boolean addStudent(String _sid, String _sname) {
        return false;
    }


    @Override
    public boolean deleteStudentBySid(String _sid) {
        boolean flag=false;
        List<String> cids = new ArrayList<>();
        //找到待删除的课程号
        for (Score score : scoreDao.findScoreBySid(_sid)) {
            if (scoreDao.findScoreNumByCid(score.getCid())==1) {
                cids.add(score.getCid());
            }
            scoreDao.deleteScoreBySid(_sid);
            studentDao.deleteStudentBySid(_sid);
            for (String cid : cids) {
                courseDao.deleteCourseByCid(cid);
            }

        }
        flag=true;
        return flag;
    }
}
